using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace IMDBScraperPlus
{
	public class IMDBParser : IParser
	{
		public string URL
		{
			get { return @"http://www.google.com/search?q={0}%20overview%20cast%20director%20site:imdb.com&btnI=I\`m Feeling Lucky"; }
		}

		public string GetTitle(string pageContent)
		{
			Match m = new Regex(@"<h1>(.*)<span>").Match(pageContent);
			return m.Groups[1].Value.Trim();
		}

		public string GetYear(string pageContent)
		{
			Match m = new Regex(@"/Sections/Years/([1-9]*\d{4})").Match(pageContent);
			return m.Groups[1].Value.Trim();
		}

		public List<string> GetGenres(string pageContent)
		{
			List<string> genres = new List<string>();

			MatchCollection mGenres = new Regex(@"(?<=/Sections/Genres/)[a-zA-Z]*(?=/"">)").Matches(pageContent);
			for (int i = 0; i < mGenres.Count - 1; i++)
				genres.Add(mGenres[i].Value);

			return genres;
		}

		public List<string> GetDirectors(string pageContent)
		{
			List<string> directors = new List<string>();

			Match mDir = new Regex(@"<h5>Director:</h5>[\r\n]<a\shref=""[\w/]*"">(?<Dir>[\w\s]*)</a>").Match(pageContent);
			foreach (Capture c in mDir.Groups[1].Captures)
				directors.Add(c.Value);

			return directors;
		}

		public List<string> GetActors(string pageContent)
		{
			List<string> actors = new List<string>();

			Match mActors = new Regex(@"Cast<(?<Actors>.*?)</table>").Match(pageContent);
			MatchCollection mActor = new Regex(@"href=\""/name/nm(?<Code>\d{7})/[""]>(?<Name>.*?)<.*?</td>.*?</td>.*?<td.*?>(?:<a href=""quotes"">){0,1}(?<Role>.*?)<").Matches(mActors.Groups["Actors"].Value);
			foreach (Match actor in mActor)
				actors.Add(actor.Groups["Name"].Value);

			return actors;
		}

		public string GetDescription(string pageContent)
		{
			Match m = new Regex("<h5>Plot:</h5>[\r\n](.*?)<a class=").Match(pageContent);
			return m.Groups[1].Value.Trim();
		}

		public string GetImageUrl(string pageContent)
		{
			Match m = new Regex(@"(<a name=""poster"".*?src="")(.*)""").Match(pageContent);
			return m.Groups[2].Value;
		}
	}
}